package com.sxz.sbs.module.client;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.BuildingNumberAdapter;
import com.sxz.sbs.module.client.bean.BuildingBean;
import com.sxz.sbs.module.client.presenter.BuildingPresenter;
import com.sxz.sbs.module.client.view.BuildingView;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 楼号列表
 */
public class BuildingNumberActivity extends BaseActivity<BuildingPresenter> implements BuildingView {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private BuildingNumberAdapter adapter;
    private List<BuildingBean.ListBean> dataList=new ArrayList<>();
    private int currentPage = 1;
    private int parkingId;

    @Override
    protected void init() {
        parkingId = getIntent().getIntExtra("parking_id",0);
        new CommonTitle.Builder(this).setTitle("选择楼号").build().show();
        refreshLayout.setColorSchemeResources(R.color.color_main);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setRefreshing(true);
        adapter = new BuildingNumberAdapter(dataList);
        recyclerView.setAdapter(adapter);
        getData();
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getData();
            }
        });
        /**
         * 上拉加载更多
         */
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage ++;
                getData();
            }
        },recyclerView);

        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                intent.putExtra("building_result",dataList.get(position));
                setResult(500,intent);
                finish();
            }
        });
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                currentPage = 1;
                getData();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_building_number;
    }
    private void getData() {

        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("parkingId", parkingId);
        map.put("pageNumber", currentPage);
        map.put("pageSize", AppConstant.PAGE_SIZE);
        //map.put("managerId",Integer.valueOf(App.preferences.getValue("managerId")));
        mPresenter.getBuildingList(map);
    }
    @Override
    protected BuildingPresenter createPresenter() {
        return new BuildingPresenter();
    }

    @Override
    public void onClientListSuccess(BuildingBean bean) {
        if (mIsLive && bean != null) {
            refreshLayout.setRefreshing(false);
            if (bean.getList() != null && bean.getList().size() >0 ) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    dataList.clear();
                    dataList.addAll(bean.getList());
                    adapter.notifyDataSetChanged();
                } else {
                    dataList.addAll(bean.getList());
                    adapter.notifyDataSetChanged();
                }
                adapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    dataList.clear();
                    adapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    adapter.loadMoreEnd();
                }
            }

        }
    }

    @Override
    public void onSubmitSuccess(String str) {

    }
    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
        adapter.loadMoreFail();
    }
    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (currentPage == 1) {
            if (errorTag == AppConstant.NET_ERROR_CODE) {
                stateLayout.showNetwork();
            } else {
                stateLayout.showEmpty();
            }
        } else {
            adapter.loadMoreFail();
        }
    }
}
